using DAL;
using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Admin
{
    public partial class Assign : Form
    {
        List<Models.t_data> ts;
        List<Models.g_data> gs;
        List<Models.Pair> pairs;
        List<Models.Pair> sorted_pair;

        int t_choice;
        int g_choice;
        public Assign()
        {
            InitializeComponent();
        }

        private void Assign_Load(object sender, EventArgs e)
        {
            toolTip1.SetToolTip(uiButton3, "点击分配");
            if (log_info.Sys_Config.tearch_choice == null || log_info.Sys_Config.group_choice==null) 
                return;
            t_choice = (int)log_info.Sys_Config.tearch_choice;
            g_choice = (int)log_info.Sys_Config.group_choice;


            BLL.B_T b_T = new BLL.B_T();
            BLL.B_G b_G = new BLL.B_G();
            
            ts = b_T.getList();//得到年级专业
            gs = b_G.getList();

            Groups_view.DataSource = new BindingList<g_data>(gs);
            Teachers_view.DataSource = new BindingList<t_data>(ts);
            count_score();//计算分数

            int start_idx = 0;
            for (int i = 5; i-start_idx > g_choice; i--)
            {
                Groups_view.Columns[i].Visible = false;
            }
         
            for (int i = 10; i - start_idx > t_choice; i--)
            {
                Teachers_view.Columns[i].Visible = false;
                Teachers_view.Columns[i + 10].Visible = false;
            }
         
            Pairs_view.Columns[0].Visible = false;
            start_idx = 2;
            for (int i = 7; i - start_idx > g_choice; i--)
            {
                Pairs_view.Columns[i].Visible = false;
            }
            start_idx = 7;
            for (int i = 17; i - start_idx > t_choice; i--)
            {
                Pairs_view.Columns[i].Visible = false;
            }

            //过滤操作
            sorted_pair = pairs.Where(p => p.score > 0).OrderByDescending(p => p.score).ToList();
            //Pairs_view.DataSource = new BindingList<Pair>(sorted_pair);
            Pairs_view.DataSource = new Tools.SortableBindingList<Pair>(sorted_pair);
        }

        void count_score()
        {
            pairs = new List<Pair>();
            foreach (var g in gs) 
            {
                foreach (var t in ts) 
                {
                    int[] S_c = { 0, 0, 0, 0, 0 };
                    int[] T_c = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

                    double marks_s = 0.0;
                    double marks_t = 0.0;

                    if (g.c1 == t.t_id) S_c[0] = 1;
                    else if(g.c2 == t.t_id) S_c[1] = 1;
                    else if(g.c3 == t.t_id) S_c[2] = 1;
                    else if(g.c4 == t.t_id) S_c[3] = 1;
                    else if(g.c5 == t.t_id) S_c[4] = 1;
                    
                    for (int n = 0; n < g_choice; n++)
                    {
                        if (S_c[n] != 0)
                        {
                            marks_s = Math.Pow(S_c[n] * 2, g_choice - n);
                            break;
                        }
                    }
                    
                    if (t.c1 == g.g_id) T_c[0] = 1;
                    else if(t.c2 == g.g_id) T_c[1] = 1;
                    else if(t.c3 == g.g_id) T_c[2] = 1;
                    else if (t.c4 == g.g_id) T_c[3] = 1;
                    else if (t.c5 == g.g_id) T_c[4] = 1;
                    else if (t.c6 == g.g_id) T_c[5] = 1;
                    else if (t.c7 == g.g_id) T_c[6] = 1;
                    else if (t.c8 == g.g_id) T_c[7] = 1;
                    else if (t.c9 == g.g_id) T_c[8] = 1;
                    else if (t.c10 == g.g_id) T_c[9] = 1;

                    for (int n = 0; n < t_choice; n++)
                    {
                        if (T_c[n] != 0)
                        {
                            marks_t = Math.Pow(T_c[n] * 2, t_choice - n);
                            break;
                        }
                    }

                    Models.Pair pair = new Models.Pair();
                    pair.group_id = g.g_id;
                    pair.teacher_id = t.t_id;
                    pair.stu_c1 = g.c1;
                    pair.stu_c2 = g.c2;
                    pair.stu_c3 = g.c3;
                    pair.stu_c4 = g.c4;
                    pair.stu_c5 = g.c5;
                    pair.teacher_c1 = t.c1;
                    pair.teacher_c2 = t.c2;
                    pair.teacher_c3 = t.c3;
                    pair.teacher_c4 = t.c4;
                    pair.teacher_c5 = t.c5;
                    pair.teacher_c6 = t.c6;
                    pair.teacher_c7 = t.c7;
                    pair.teacher_c8 = t.c8;
                    pair.teacher_c9 = t.c9;
                    pair.teacher_c10 = t.c10;
                    pair.score = marks_s + marks_t * 1.1;
                    pair.assigned = 0;

                    pairs.Add(pair);
                }
            }

            //Pairs_view.DataSource = new BindingList<Pair>(pairs);
            Pairs_view.DataSource = new Tools.SortableBindingList<Pair>(pairs);

        }

        private void Filter_Click(object sender, EventArgs e)
        {
            sorted_pair = pairs.Where(p=>p.score > 0).OrderByDescending(p=>p.score).ToList();
            //Pairs_view.DataSource = new BindingList<Pair>(sorted_pair);
            Pairs_view.DataSource = new Tools.SortableBindingList<Pair>(sorted_pair);//为了让学生组都排在一起，绑定排序

        }

        private void Auto_pair_Click(object sender, EventArgs e)
        {
            clear_assigned();//把所有标记位清空
            int assigned, t_rescount;

            foreach (var pair in sorted_pair)
            {
                assigned = (int)pair.assigned;
                if (assigned == 1) continue; //如果这一行已经经过分配运算，则下一行

                Models.t_data teacher = ts.Where(t=>t.t_id == pair.teacher_id).FirstOrDefault();
                t_rescount = teacher.res_count;
                if (t_rescount == t_choice) continue;//如果这个老师的名额已满，则下一行

                Models.g_data group =  gs.Where(g=>g.g_id== pair.group_id).FirstOrDefault();

                for(int i=1;i <= t_choice; i++)
                {
                    if(get_tChoice(teacher, $"g{i}")==-1)
                    {
                        set_tChoice(teacher, $"g{i}", group.g_id);
                        break;
                    }
                }
                
                                         
                //if(teacher.g1==-1)  teacher.g1 = group.g_id;
                //else if(teacher.g2 == -1) teacher.g2 = group.g_id;
                //else if(teacher.g3 == -1) teacher.g3 = group.g_id;
                //else if(teacher.g4 == -1) teacher.g4 = group.g_id;
                //else if(teacher.g5 == -1) teacher.g5 = group.g_id;

                teacher.res_count += 1;
                group.t_id = teacher.t_id;
                pairs.Where(p => p.group_id == group.g_id).ToList().ForEach(p => p.assigned = 1);
            }
           // BLL.B_G b_G = new BLL.B_G();
           // BLL.B_T b_T = new BLL.B_T();
           //
           // b_G.save_result(gs);
           // b_T.save_result(ts);

            Groups_view.DataSource = new BindingList<g_data>(gs);
            Teachers_view.DataSource = new BindingList<t_data>(ts);
        }

        private int get_tChoice(Models.t_data t_Data, string pro_name)
        {
            return (int)t_Data.GetType().GetProperty(pro_name).GetValue(t_Data, null); ;
        }

        private void set_tChoice(Models.t_data t_Data, string pro_name, int g_id)
        {
            t_Data.GetType().GetProperty(pro_name).SetValue(t_Data, g_id);
        }

        private void clear_assigned()
        {
            ts.ForEach(t => { t.g1 = -1; t.g2 = -1; t.g3 = -1;t.g4 = -1;t.g5 = -1; t.g6 = -1;t.g7 = -1;t.g8 = -1;t.g9 = -1;t.g10 = -1; t.res_count = 0; });
            gs.ForEach(g => { g.t_id = -1; });
        }

        private void Release_Click(object sender, EventArgs e)
        {
            BLL.B_Release_Result b_Release_Result = new BLL.B_Release_Result();
            try
            {
                var res = b_Release_Result.save_result(ts, gs);
                if (res) MessageBox.Show("发布成功!");
            }
            catch (Exception ex) 
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ID_Name_Click(object sender, EventArgs e)
        {
            View_IDs view_Data = new View_IDs();
            view_Data.Show();
        }

        private void Manual_adjust_Click(object sender, EventArgs e)
        {
            Manual_adjust manual_Adjust = new Manual_adjust();
            manual_Adjust.ShowDialog();
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            View_IDs view_Data = new View_IDs();
            view_Data.Show();
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            Manual_adjust manual_Adjust = new Manual_adjust();
            manual_Adjust.ShowDialog();
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            clear_assigned();//把所有标记位清空
            int assigned, t_rescount;

            foreach (var pair in sorted_pair)
            {
                assigned = (int)pair.assigned;
                if (assigned == 1) continue; //如果这一行已经经过分配运算，则下一行

                Models.t_data teacher = ts.Where(t => t.t_id == pair.teacher_id).FirstOrDefault();
                t_rescount = teacher.res_count;
                if (t_rescount == t_choice) continue;//如果这个老师的名额已满，则下一行

                Models.g_data group = gs.Where(g => g.g_id == pair.group_id).FirstOrDefault();

                for (int i = 1; i <= t_choice; i++)
                {
                    if (get_tChoice(teacher, $"g{i}") == -1)
                    {
                        set_tChoice(teacher, $"g{i}", group.g_id);
                        break;
                    }
                }


                //if(teacher.g1==-1)  teacher.g1 = group.g_id;
                //else if(teacher.g2 == -1) teacher.g2 = group.g_id;
                //else if(teacher.g3 == -1) teacher.g3 = group.g_id;
                //else if(teacher.g4 == -1) teacher.g4 = group.g_id;
                //else if(teacher.g5 == -1) teacher.g5 = group.g_id;

                teacher.res_count += 1;
                group.t_id = teacher.t_id;
                pairs.Where(p => p.group_id == group.g_id).ToList().ForEach(p => p.assigned = 1);
            }
            // BLL.B_G b_G = new BLL.B_G();
            // BLL.B_T b_T = new BLL.B_T();
            //
            // b_G.save_result(gs);
            // b_T.save_result(ts);

            Groups_view.DataSource = new BindingList<g_data>(gs);
            Teachers_view.DataSource = new BindingList<t_data>(ts);
        }

        private void uiButton4_Click(object sender, EventArgs e)
        {
            BLL.B_Release_Result b_Release_Result = new BLL.B_Release_Result();
            try
            {
                var res = b_Release_Result.save_result(ts, gs);
                if (res) MessageBox.Show("发布成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
